package org.tonny.algorithm.leetcode;

import java.util.*;

/**
 * 209. 长度最小的子数组
 */
public class LeetCode209 {
    public int minSubArrayLen(int target, int[] nums) {
        int minLen = nums.length + 1;
        int l = 0, r = -1, sum = 0;
        while (l < nums.length) {
            if (r + 1 < nums.length && sum < target) {
                sum = sum + nums[++r];
            } else {
                sum = sum - nums[l++];
            }
            if (sum >= target) {
                minLen = Math.min(minLen, r - l + 1);
            }
        }
        if (minLen != nums.length + 1) {
            return minLen;
        }
        return 0;
    }
}